<!DOCTYPE html>
<html>
  <head>
    <title>IOT LAO WANG</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  </head>
  <body>
    <div class="topnav">
      <h1>ESP WEB SERVER - Control Multiple Outputs</h1>
    </div>
    <div class="content">
      <div class="card-grid">
        <div class="card">             
          <p class="card-title"><i class="fas fa-lightbulb"></i> GPIO 2</p>
          <label class="switch">
            <input type="checkbox" onchange="toggleCheckbox(this)" id="2">
            <span class="slider"></span>
          </label>
          <p class="state">State: <span id="2s"></span></p>
        </div>
        <div class="card">
          <p class="card-title"><i class="fas fa-lightbulb"></i> GPIO 4</p>
          <label class="switch">
            <input type="checkbox" onchange="toggleCheckbox(this)" id="4">
            <span class="slider"></span>
          </label>
          <p class="state">State: <span id="4s"></span></p>
        </div>
        <div class="card">
          <p class="card-title"><i class="fas fa-lightbulb"></i> GPIO 14</p>
          <label class="switch">
            <input type="checkbox" onchange="toggleCheckbox(this)" id="14">
            <span class="slider"></span>
          </label>
          <p class="state">State: <span id="14s"></span></p>
        </div>
        <div class="card">
          <p class="card-title"><i class="fas fa-lightbulb"></i> GPIO 12</p>
          <label class="switch">
            <input type="checkbox" onchange="toggleCheckbox(this)" id="12">
            <span class="slider"></span>
          </label>
          <p class="state">State: <span id="12s"></span></p>
        </div>
      </div>
    </div>
    <script>
      // Get current GPIO states when the page loads  
      window.addEventListener('load', getStates);
    // Function to get and update GPIO states on the webpage when it loads for the first time
      function getStates(){
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            var myObj = JSON.parse(this.responseText);
            console.log(myObj);
            for (i in myObj.gpios) {
              var output = myObj.gpios[i].output;
              var state = myObj.gpios[i].state;
              console.log(output);
              console.log(state);
              if (state == "1") {
                document.getElementById(output).checked = true;
                document.getElementById(output+"s").innerHTML = "ON";
              }
              else {
                document.getElementById(output).checked = false;
                document.getElementById(output+"s").innerHTML = "OFF";
              }
            }
          }
        }; 
        xhr.open("GET", "/states", true);
        xhr.send();
      }

      // Send Requests to Control GPIOs
      function toggleCheckbox (element) {
        var xhr = new XMLHttpRequest();
        if (element.checked) {
          xhr.open("GET", "/update?output="+element.id+"&state=1", true);
          document.getElementById(element.id+"s").innerHTML = "ON";
        }
        else {
          xhr.open("GET", "/update?output="+element.id+"&state=0", true);
          document.getElementById(element.id+"s").innerHTML = "OFF"; 
        }
        xhr.send();
      }
    </script>
  </body>
</html>